package com.zt.bus.line.controller;

import com.zt.bus.line.model.LinePlan;
import com.zt.bus.line.model.LinePlanDiscount;
import com.zt.bus.line.model.LinePlanRefund;
import com.zt.bus.line.model.LinePlanSchedule;
import com.zt.bus.line.service.LinePlanService;
import com.zt.bus.order.service.OrderService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import com.zt.system.model.BusinessException;
import com.zt.util.DateUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/linePlan")
public class LinePlanController {

    @Autowired
    private LinePlanService linePlanService;
    
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(linePlanService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject addLinePlan(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        linePlanService.add(getLinePlan(reqBean), getSchedules(reqBean), getLinePlanDiscount(reqBean),
                getLinePlanRefund(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/getByLineId")
    @ResponseBody
    public JSONObject getByLineId(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.responseSuccess(linePlanService.getByLineId(reqBean.getInfo("lineId")));
    }

    @RequestMapping(value = "/beforeUpdate")
    @ResponseBody
    public JSONObject beforeUpdate(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.responseSuccess(linePlanService.beforeUpdate(reqBean.getInfo("planId"),
                reqBean.getInfo("planStatus")));
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject updateLinePlan(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        linePlanService.update(getLinePlan(reqBean), getSchedules(reqBean), getLinePlanDiscount(reqBean),
                getLinePlanRefund(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject deleteLinePlan(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        linePlanService.delete(reqBean.getInfo("planId"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/detail")
    @ResponseBody
    public JSONObject getDetailByPlanId(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        String planId = reqBean.getInfo("planId");
        JSONObject jsonObject = new JSONObject();
        List<LinePlanSchedule> list = linePlanService.getLinePlanSchedule(planId);
        JSONArray array = new JSONArray();
        for (LinePlanSchedule schedule : list) {
            JSONObject json = new JSONObject();
            json.put("date", DateUtil.formatDate(schedule.getDepartDate()));
            array.add(json);
        }
        jsonObject.put("schedules", array);

        array = new JSONArray();
        for (LinePlanDiscount linePlanDiscount : linePlanService.getLinePlanDiscounts(planId)) {
            array.add(linePlanDiscount.toJSONObject());
        }
        jsonObject.put("discounts", array);
        LinePlanRefund linePlanRefund = linePlanService.getLinePlanRefund(planId);
        jsonObject.put("refund", linePlanRefund == null ? new JSONObject() : linePlanRefund.toJSONObject());
        return Response.response(jsonObject);
    }

    /**
     * 获取方案可预订日期
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/canBookDays")
    @ResponseBody
    public JSONObject getCanBookDays(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        String planId = reqBean.getInfo("planId");
        List<Date> list = linePlanService.getCanBookDays(planId, new Date());
        JSONArray array = linePlanService.getBookableSeatingNumOfPlan(planId, list);
        return Response.response(array);
    }

    /**
     * 从表单获取对象 线路方案
     * 
     * @param reqBean
     * @return
     */
    private LinePlan getLinePlan(ReqBean reqBean) {
        LinePlan linePlan = new LinePlan();
        linePlan.setPlanId(reqBean.getInfo("planId"));
        linePlan.setLineId(reqBean.getInfo("lineId"));
        linePlan.setStartDate(reqBean.getInfoDate("startDate"));
        linePlan.setDepartTime(reqBean.getInfo("departTime"));
        linePlan.setBeforeMinute(reqBean.getInfoInt("beforeMinute"));
        linePlan.setSeating(reqBean.getInfoInt("seating"));
        linePlan.setPlanStatus(reqBean.getInfo("planStatus"));
        linePlan.setOperator(reqBean.getUserId());
        linePlan.setRemark(reqBean.getInfo("remark"));
        return linePlan;
    }

    private List<Date> getSchedules(ReqBean reqBean) {
        String[] dates = reqBean.getInfo("schedules").split(",");
        if (dates.length == 0) {
            throw new BusinessException("请选择发车日期");
        }
        List<Date> list = new ArrayList<>();
        for (String date : dates) {
            list.add(DateUtil.parse(date));
        }
        return list;
    }

    /**
     * 从表单获取对象 线路方案折扣方案
     * 
     * @param reqBean
     * @return
     */
    private List<LinePlanDiscount> getLinePlanDiscount(ReqBean reqBean) {
        List<LinePlanDiscount> list = new ArrayList<>();
        JSONArray array = JSONArray.fromObject(reqBean.getInfo("discounts"));
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            LinePlanDiscount planDiscount = new LinePlanDiscount();
            planDiscount.setId(jsonObject.optString("id"));
            // planDiscount.setPlanId(jsonObject.getString("planId"));
            planDiscount.setDiscountId(jsonObject.getString("discountId"));
            planDiscount.setExpirationDate(DateUtil.parse(jsonObject.getString("expirationDate")));
            if (planDiscount.getExpirationDate() == null) {
                throw new BusinessException("请设置折扣有效日期！");
            }
            planDiscount.setOperator(reqBean.getUserId());
            list.add(planDiscount);
        }
        return list;
    }

    /**
     * 从表单获取对象 线路方案退款方案
     * 
     * @param reqBean
     * @return
     */
    private LinePlanRefund getLinePlanRefund(ReqBean reqBean) {
        JSONObject jsonObject = JSONObject.fromObject(reqBean.getInfo("refund"));
        LinePlanRefund linePlanRefund = new LinePlanRefund();
        linePlanRefund.setId(jsonObject.optString("id"));
        // linePlanRefund.setPlanId(jsonObject.getString("planId"));
        linePlanRefund.setRefundId(jsonObject.getString("refundId"));
        linePlanRefund.setExpirationDate(DateUtil.parse(jsonObject.getString("expirationDate")));
        linePlanRefund.setOperator(reqBean.getUserId());
        return linePlanRefund;
    }

    /**
     * 查询订单，后台管理
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/getAllLinePlan")
    @ResponseBody
    public JSONObject getAllLinePlan(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	return Response.response(linePlanService.getAllLinePlan());
    }
    
}
